var maintainVm = new Vue({
	el:'#equip-maintain-content',
	data: function() {
		return {
			
			showEquipmentDetail: false,
			//资产信息
			equipmentid: '',
			zcbh: '',
			tmbh: '',
			zybbh: '',
			type: '',
			name: '',
			vendor: '',
			model: '',
			depart:'',
			workshop: '',
			user: '',
			contact: '',
			location: '',
			note: '',
			recorder: '',
			putindate: '',
			state: '',
			// 维修信息
			maintainid: '',
			stateError: '',
			stateM:'',
			surveyorError: '',
			surveyor: '',
			thecontactError: '',
			thecontact:'',
			contactError: '',
			contactM: '',
			descError: '',
			descM: '',
			recorddateM: '',
			submitTxt: '提交',
			showMaintainDate: false,
			readOnly: false
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		mui.plusReady(function () {
			var lastPage = plus.webview.getWebviewById('equipManage');
			if(lastPage) lastPage.close();
			
			var thisPage = plus.webview.currentWebview();
			self.equipmentid = thisPage.equipmentid;
			self.maintainid = thisPage.maintainid;
			self.isInMaintain = thisPage.isInMaintain;
			
			console.log("id来啦: " + thisPage.maintainid)
			self.initLayout();
			self.initValidator();
			self.getEquipmentBaseData();
			self.initStateData();
			if(self.isInMaintain) {
				self.showMaintainDate = true;
				self.submitTxt = '关闭';
				self.readOnly = 'readonly';
				self.initMaintainData();
			}
		})
	},
	methods:{
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		initValidator: function() {
			this.constraints = {
				stateM: {
					presence: true,
				},
				surveyor: {
					presence: true,
				},
				thecontact: {
					presence: true,
					length: {
						maximum: 13,
						message: "长度不能超过13"
					}
				},
				contactM: {
					presence: true,
					length: {
						maximum: 60,
						message: "长度不能超过60"
					}
				}
			}
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		getEquipmentBaseData: function() {
			var self = this;
			var url = config.requestBaseUrl + "getEquipmentBaseInfoById";
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					var thisData = data.data;
					if(data.isSuccess) {
						self.zcbh = thisData.zcbh;
						self.tmbh = thisData.tmbh;
						self.zybbh = thisData.zybbh;
						self.type = thisData.type;
						self.name = thisData.name;
						self.vendor = thisData.vendor;
						self.model = thisData.model;
						self.depart = thisData.depart;
						self.workshop = thisData.workshop;
						self.user = thisData.user;
						self.contact = thisData.contact;
						self.location = thisData.location;
						self.note = thisData.note;
						self.recorder = thisData.recorder;
						self.putindate = thisData.putindate;
						self.state = thisData.state;
					}
				},
				error:function(xhr,type,errorThrown){
					console.log("失败")
				}
			});
		},
		initStateData: function() {
			var self = this;
			var url = config.requestBaseUrl + "getSystemStandData";
			this.statePicker = new mui.PopPicker();
			
			mui.ajax(url,{
				data:{
					type: 'maintainState'
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					var pickerData = [];
					if(data.isSuccess) {
						data.data.forEach(function(item, index) {
							pickerData.push({text: item.name, value:item.value});
						})
					}
					self.statePicker.setData(pickerData);
				},
				error:function(xhr,type,errorThrown){
					console.log("出错啦:" + errorThrown);
					mui.toast("出错啦:" + errorThrown);
				}
			});
		},
		showInfoDetail: function(boo) {
			this.showEquipmentDetail = boo;
		},
		initMaintainData: function() {
			
			var self = this;
			var url = config.requestBaseUrl + 'getEquipmentStateInfo';
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid,
					state: '3' // 送修中
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					var thisData = data.data;
					if(data.isSuccess) {
						self.stateM = thisData.state;
						self.surveyor = thisData.surveyor;
						self.thecontact = thisData.thecontact;
						self.contactM = thisData.contact;
						self.recorddateM = thisData.recorddate.split('T')[0];
						self.descM = thisData.desc;
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					console.log(errorThrown)
					console.log("失败")
				}
			});
		},
		stateTypePicker: function() {
			var self = this;
			this.statePicker.show(function (selectItems) {
				self.stateM = selectItems[0].text;
			})
		},
		// surveyorTypePicker: function() {
		// 	var self = this;
		// 	var picker = new mui.PopPicker();
		// 	picker.setData([{value:'1',text:'付小雨'},{value:'2',text:'谭梦'},{value:'3',text:'熊普普'}]);
		// 	picker.show(function (selectItems) {
		// 		self.surveyor = selectItems[0].text;
		// 	})
		// },
		submitMaintain: function() {
			var self = this;
			var data = {};
			if(this.stateM) {
				data['stateM'] = this.stateM;
			}
			if(this.surveyor) {
				data['surveyor'] = this.surveyor;
			}
			if(this.thecontact) {
				data['thecontact'] = this.thecontact;
			}
			if(this.contactM) {
				data['contactM'] = this.contactM;
			}
			var errors = validate(data, this.constraints);
			console.log(errors)
			if(errors) {
				for(x in errors) {
					console.log(x + ":" + errors[x])
				}
				if(errors.stateM) {
					this.stateError = errors.stateM;
				}
				if(errors.surveyor) {
					this.surveyorError = errors.surveyor;
				}
				if(errors.thecontact) {
					this.thecontactError = errors.thecontact;
				}
				if(errors.contactM) {
					this.contactError = errors.contactM;
				}
			} else {
				if(this.isInMaintain) {
					mui.confirm('是否确认关闭该资产的送修状态?','提示',['取消','确认'],function (e) {
						if(e.index == 1) {
							self.closeMaintain();
						}
					},'div')
				} else {
					this.submitData();
				}
			}
		},
		closeMaintain: function() {
			var self = this;
			var thisPage = plus.webview.currentWebview();
			var url = config.requestBaseUrl + 'closeMaintain';
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					mui.toast(data.msg);
					if(data.isSuccess) {
						var indexPage = plus.webview.getLaunchWebview();
						mui.fire(indexPage,'refresh',{});
						thisPage.close();
					}
				},
				error:function(xhr,type,errorThrown){
					console.log(errorThrown)
					mui.toast("出错!请重试或联系管理员");
				}
			});
		},
		submitData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'addMaintain';
			var recorderid = plus.storage.getItem('userid');
			var thisPage = plus.webview.currentWebview();
			
			
			console.log(this.equipmentid)
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid,
					state: this.state,
					surveyor: this.surveyor,
					thecontact: this.thecontact,
					contact: this.contactM,
					recorderid: recorderid,
					desc: this.descM,
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						mui.toast("故障上报成功!");
						var indexPage = plus.webview.getLaunchWebview();
						mui.fire(indexPage,'refresh',{});
						
						thisPage.close();
					} else {
						mui.toast("出错!请重试或联系管理员");
					}
				},
				error:function(xhr,type,errorThrown){
					console.log('失败' + errorThrown)
					mui.toast("出错!请重试或联系管理员");
				}
			});
		}
	}
})